package com.xunua.NaoZiKongBai.Base;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import com.xunua.NaoZiKongBai.Bean.EventBus.RealtimeloginstatusEvent;
import com.xunua.NaoZiKongBai.LoginActivity.LoginActivity;
import com.xunua.NaoZiKongBai.MyApplication;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

/**
 * @user XUNUA
 * @email xunua@outlook.com
 * @creat time 2020/5/30 1:25
 */
public class MyActivity extends SwipeBackActivity {
    private MyApplication myApplication;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        myApplication = (MyApplication) getApplication();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true,priority = 1)
    public void onRealtimeloginstatusEvent(RealtimeloginstatusEvent event){
        if (event.isPwdFaild()) {
            //密码错误
            //需要进行提示框  让用户退回到登录页面
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setCancelable(false);
            builder.setTitle("提示").setMessage("账号状态异常，即将下线，请重新登录").setPositiveButton("ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (myApplication.activityList.size() != 1) {
                        myApplication.finishAllActivity();//销毁所有activity
                    }else {
                        String simpleName = myApplication.activityList.get(0).getClass().getSimpleName();
                        if (simpleName.equals("LoginActivity")) {
                            EventBus.getDefault().removeStickyEvent(event);//移除粘性事件
                            return;
                        }
                    }
                    //可以使用intent来打开用户登陆界面
                    Intent intentLogin = new Intent(getApplicationContext(), LoginActivity.class);
                    startActivity(intentLogin);
                    EventBus.getDefault().removeStickyEvent(event);//移除粘性事件
                    finish();
                }
            }).show();
        }
    }
}
